package com.oracleoaec.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBUtils {
	@SuppressWarnings("unused")
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	
	//静态代码块
	static{
		Properties p=new Properties();
		InputStream is=DBUtils.class.getResourceAsStream("driver.properties");
		try {
			p.load(is);
			driver=p.getProperty("driver");
			url=p.getProperty("url");
			user=p.getProperty("username");
			password=p.getProperty("password");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//获取连接
	public static Connection getConn(){
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			System.out.println("连接失败");
		}
		return conn;
	}
	
	//关闭连接
	public static void close(ResultSet rs,Statement st,Connection conn){
		try {
			if(rs!=null)
				rs.close();
			if(st!=null)
				st.close();
			if(conn!=null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void close(Statement stmt,Connection conn){
		//调用上面重载的close方法即可,不需要传null
		close(null,stmt,conn);
	}
	
	public static void close(Statement stmt){
		close(null,stmt,null);
	}
	
	public static void close(ResultSet rs,Statement stmt){
		close(rs,stmt,null);
	}
	
	public static void close(Connection conn){
		close(null,null,conn);
	}
}
